home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / SINEWAVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  2.8 KB  |  93 lines

  1. /*-----------------------------------------
  2.    SINEWAVE.C -- Sine Wave Using Polyline
  3.                  (c) Charles Petzold, 1996
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8.  
  9. #define NUM    1000
  10. #define TWOPI  (2 * 3.14159)
  11.  
  12. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16.      {
  17.      static char szAppName[] = "SineWave" ;
  18.      HWND        hwnd ;
  19.      MSG         msg ;
  20.      WNDCLASSEX  wndclass ;
  21.  
  22.      wndclass.cbSize        = sizeof (wndclass) ;
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  31.      wndclass.lpszMenuName  = NULL ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  34.  
  35.      RegisterClassEx (&wndclass) ;
  36.  
  37.      hwnd = CreateWindow (szAppName, "Sine Wave Using Polyline",
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.  
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.           {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.           }
  51.      return msg.wParam ;
  52.      }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  55.      {
  56.      static int  cxClient, cyClient ;
  57.      HDC         hdc ;
  58.      int         i ;
  59.      PAINTSTRUCT ps ;
  60.      POINT       pt [NUM] ;
  61.  
  62.      switch (iMsg)
  63.           {
  64.           case WM_SIZE:
  65.                cxClient = LOWORD (lParam) ;
  66.                cyClient = HIWORD (lParam) ;
  67.                return 0 ;
  68.  
  69.           case WM_PAINT:
  70.                hdc = BeginPaint (hwnd, &ps) ;
  71.  
  72.                MoveToEx (hdc, 0,        cyClient / 2, NULL) ;
  73.                LineTo   (hdc, cxClient, cyClient / 2) ;
  74.  
  75.                for (i = 0 ; i < NUM ; i++)
  76.                     {
  77.                     pt[i].x = i * cxClient / NUM ;
  78.                     pt[i].y = (int) (cyClient / 2 *
  79.                                     (1 - sin (TWOPI * i / NUM))) ;
  80.                     }
  81.  
  82.                Polyline (hdc, pt, NUM) ;
  83.  
  84.                return 0 ;
  85.  
  86.           case WM_DESTROY:
  87.                PostQuitMessage (0) ;
  88.                return 0 ;
  89.           }
  90.  
  91.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  92.      }
  93.